/*
    Copyright 2011 Peter Pratscher
 
    This file is part of Surveillance.NET.

    Surveillance.NET is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    Surveillance.NET is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Surveillance.NET. If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Net.Mail;
using System.Runtime.Serialization;
using ActiveUp.Net.Mail;

namespace SurveillanceDotNet.Model
{
    [Serializable]
    public class GMailMailAccount : MailAccount
    {
        public GMailMailAccount()
            : base()
        {
        }

        public override void SendMail(MailAddress toAddress, string subject, string body, List<System.Net.Mail.Attachment> attachments)
        {
            var smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                Credentials = new NetworkCredential(FromAddress.Address, Password),
                Timeout = 20000
            };
            using (var message = new MailMessage(FromAddress, toAddress)
            {
                Subject = subject,
                Body = body
            })
            {
                foreach (System.Net.Mail.Attachment a in attachments)
                {
                    message.Attachments.Add(a);
                }

                smtp.Send(message);
            }
        }

        public override void CheckNewMail()
        {
            try
            {
                Pop3Client pop3Client = new Pop3Client();

                pop3Client.ConnectSsl("pop.gmail.com", 995, this.FromAddress.Address, Password);

                for (int i = 1; i <= pop3Client.MessageCount; i++)
                {
                    //Retrieve and delete the message
                    Message msg = pop3Client.RetrieveMessageObject(i, true);

                    //Fire the event
                    OnNewMail(new NewMailEventArgs()
                    {
                        Message = msg
                    });
                }

                pop3Client.Disconnect();
                pop3Client.Close();
            }
            catch (Exception ex) //Error during EMail retrieval
            {
                Debug.WriteLine(ex.ToString());
            }
        }

        public override string ToString()
        {
            return "GMail - " + this.FromAddress.Address;
        }

        #region ISerializable Implementation
        protected GMailMailAccount(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
        }
        #endregion
    }
}
